package com.itpeng.mobile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itpeng.Utils.SMSUtils;
import com.itpeng.Utils.ValidateCodeUtils;
import com.itpeng.constant.MessageConstant;
import com.itpeng.constant.RedisMessageConstant;
import com.itpeng.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/25  19:56
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

   /* @Reference
    private ValidateCodeDao validateCodeDao;*/

    /**
     * 发送验证码
     * @param telephone
     * @return
     */
    @GetMapping("/send4Order")
    public Result send4Order(String telephone){
        Jedis jedis = jedisPool.getResource();
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //1. 先从redis缓存查询是否存在该号码所对应的验证码
        String codeInRedis = jedis.get(key);
        //2. 判断code是否存在
        if (codeInRedis == null){
            //若不存在，则产生验证码
            String code = ValidateCodeUtils.generateValidateCode4String(6);
            try {
                //3. 发送验证码
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, code);
                //4. 发送成功后，将验证码存入Redis中
                jedis.set(key,code);
                //5.返回结果
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (ClientException e) {
                e.printStackTrace();
                //若出现异常则提示发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        //若存在，则提示用户验证码已发送
        return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
    }

    /**
     *
     * @return
     */
    @GetMapping("/send4Login")
    public Result send4Login(String telephone){
        Jedis jedis = jedisPool.getResource();
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        //1. 先从redis缓存查询是否存在该号码所对应的验证码
        String codeInRedis = jedis.get(key);
        //2. 判断code是否存在
        if (codeInRedis == null){
            //若不存在，则产生验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            try {
                //3. 发送验证码
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, code+"");
                //4. 发送成功后，将验证码存入Redis中
                jedis.set(key,code+"");
                //5.返回结果
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (ClientException e) {
                e.printStackTrace();
                //若出现异常则提示发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        //若存在，则提示用户验证码已发送
        return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
    }
}
